<?php

namespace app\admin\controller\finance;

use app\admin\controller\Controller;
use app\agent\service\statistics\Data as StatisticsDataService;
use app\admin\service\User;
use app\admin\service\Order;
use app\admin\service\Goods;
use app\admin\service\Trade7days;
/**
 * 数据概况
 * Class Data
 * @package app\store\controller\statistics
 */
class Data extends Controller
{


    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 数据统计主页
     * @return mixed
     * @throws \think\Exception
     */
    public function index()
    {
        return $this->fetch('index', [
            // 数据概况
            'survey' => $this->getSurveyData(),
            // 近七日交易走势
            'echarts7days' => $this->getTransactionTrend(),
            // 商品销售榜
            'goodsRanking' => $this->getGoodsRanking(),
            // 用户消费榜
            'userExpendRanking' => $this->geUserExpendRanking(),
        ]);
    }

    /**
     * 数据概况API
     * @param null $startDate
     * @param null $endDate
     * @return array
     * @throws \think\Exception
     */
    public function survey($startDate = null, $endDate = null)
    {
        return $this->renderSuccess('', '',
            $this->getSurveyData($startDate, $endDate));
    }

    // 数据概况
    private function getSurveyData($startDate = null, $endDate = null){
        return [
            // 用户数量
            'user_total' => (new User)->getUserTimeTotal($startDate, $endDate),
            // 消费人数
            'consume_users' =>(new Order)->getConsumeUsers($startDate, $endDate),
            // 付款订单数
            'order_total' => (new Order)->getPayOrderTotal($startDate, $endDate),
            // 付款订单总额
            'order_total_money' =>(new Order)->getOrderTotalMoney($startDate, $endDate),
            // 商品总量
            'goods_total' => (new Goods)->getGoodsTotal($startDate, $endDate),
            // 用户充值总额
            'recharge_total' =>(new User)->getRechargeTotal($startDate, $endDate),
        ];
    }
    // 近七日交易走势
    private function getTransactionTrend(){
        return (new Trade7days)->getTransactionTrend();
    }
    // 商品销售榜
    private function getGoodsRanking(){
        return db('order_goods')->alias('o_goods')
            ->field([
                'goods_id',
                'goods_name',
                'SUM(total_pay_price) AS sales_volume',
                'SUM(total_num) AS total_sales_num'
            ])
            ->join('order', 'order.order_id = o_goods.order_id')
            ->where('order.pay_status', '=', 20)
            ->where('order.order_status', '<>', 20)
            ->group('goods_id, goods_name')
            // order：此处按总销售额排序，如需按销量改为total_sales_num
            ->order(['sales_volume' => 'DESC'])
            ->limit(10)
            ->select();
    }
    // 用户消费榜
    private function geUserExpendRanking(){
        return db('user')->field(['user_id', 'nickName', 'expend_money'])
            ->where('is_delete', '=', 0)
            ->order(['expend_money' => 'DESC'])
            ->limit(10)
            ->select();
    }

}